/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_hwi.h"
#include "los_task.h"
#include "los_api_bsp_gmc.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "netif/etharp.h"
#include "ethernetif.h"
#include "lwip/tcpip.h"
#include "pcram.h"
#include "lwip/sockets.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_uwTskHiID;
static UINT32 g_uwTskLoID;
#define TSK_PRIOR_HI 4
#define TSK_PRIOR_LO 5
static UINT8 g_XEC_flag=1;

#define SERVER_ADDR "192.168.116.100"
#define SELF_ADDR "192.168.116.11"
#define SERVER_PORT 1883
#define BUFFER_SIZE 128

#define PORT           1883
#define IP_ADDR        "192.168.116.100"
#define RECV_DATA         (1024)
void ipv4_tcp_test(void)
{

    int rbytes = -1;
  int sock = -1;
  struct sockaddr_in client_addr;
  
  uint8_t send_buf[]= "This is a TCP Client test...\n";
  
  while(1)
  {
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
      printf("Socket error\n");
      LOS_TaskDelay(1000);
      continue;
    } 
 
    client_addr.sin_family = AF_INET;      
    client_addr.sin_port = htons(PORT);   
    client_addr.sin_addr.s_addr = inet_addr(IP_ADDR);
    memset(&(client_addr.sin_zero), 0, sizeof(client_addr.sin_zero));    
 
    if (connect(sock, 
               (struct sockaddr *)&client_addr, 
                sizeof(struct sockaddr)) == -1) 
    {
        printf("Connect failed!\n");
        closesocket(sock);
        LOS_TaskDelay(10);
        continue;
    }                                           
    
    printf("Connect to iperf server successful!\n");
    
    while (1)
    {
      if(send(sock,send_buf,sizeof(send_buf),0) < 0)
        break;
        char recv_buf[50]={0};
        while( -1 == (rbytes = recv(sock, recv_buf, 50, 0)));
        printf("recv [%d] bytes: %s.\r\n", rbytes, recv_buf);
      LOS_TaskDelay(3000);
    }
    
    closesocket(sock);
  }
}

void ipv4_tcp_server_test(void)
{
    
  printf("ipv4_tcp_server_test\n");

  int sock = -1,connected;
  char *recv_data;
  struct sockaddr_in server_addr,client_addr;
  socklen_t sin_size;
  int recv_data_len;
  char recv_buf[RECV_DATA]={0};
  recv_data = recv_buf;
  if (recv_data == NULL)
  {
      printf("No memory\n");
      goto __exit;
  }
  
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
  {
      printf("Socket error\n");
      goto __exit;
  }
  
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(PORT);
 
  memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  
  if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  {
      printf("Unable to bind\n");
      goto __exit;
  }
  
  if (listen(sock, 5) == -1)
  {
      printf("Listen error\n");
      goto __exit;
  }
  
  while(1)
  {
    sin_size = sizeof(struct sockaddr_in);
 
   // connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
	if ((connected = accept(sock, (struct sockaddr *)&client_addr, (socklen_t *)&sin_size)) == -1)
	{
		printf("accept error,errno 0x%x\r\n",errno);
		continue;
	}
	
    printf("new client connected from (%s, %d)\n",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    {
      int flag = 1;
      
      setsockopt(connected,
                 IPPROTO_TCP,     /* set option at TCP level */
                 TCP_NODELAY,     /* name of option */
                 (void *) &flag,  /* the cast is historical cruft */
                 sizeof(int));    /* length of option value */
    }
    while(1)
    {
      recv_data_len = recv(connected, recv_data, RECV_DATA, 0);
      
      if (recv_data_len <= 0) 
        break;
      
      printf("recv %d len data\n",recv_data_len);
      
      send(connected,recv_data,recv_data_len,0);
      
    }
    if (connected >= 0) 
      closesocket(connected);
    
    connected = -1;
  }
__exit:
  if (sock >= 0) closesocket(sock);
}


void ipv4_udp_test(void)
{
    //printf("rudy sample_udp_demo !!\r\n");
    //sample_udp_demo();
    
	char recv_buf[50]={0};
	
    printf("rudy udp test 12 !!\r\n");    
    printf("demo ip4:%d, ip6:%d", LWIP_IPV4, LWIP_IPV6);
    struct sockaddr_in server_addr;
	int    sockAddrSize;
    char msg[128] = "Hi ipv4 server Rudy";
    int rbytes = -1;

    int client_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (client_fd < 0) {
        printf("client_fd is %d\n", client_fd);
        return;
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    if (inet_pton(AF_INET, SERVER_ADDR, &server_addr.sin_addr) <= 0) {
        printf("inet_pton error!!!\n");
        return;
    }
    
    LOS_TaskDelay(1500);

   sockAddrSize = sizeof (struct sockaddr_in);
   while(1)
   	{
	   rbytes = sendto(client_fd, msg, strlen(msg), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr_in));

	    if (rbytes > 0) {
	        printf("send to server: %d bytes written\n> %s\n", rbytes, msg);
	    } else {
	        printf("send faile\n");
	    }

	    while( -1 == (rbytes  = recvfrom(client_fd, recv_buf, 50, 0,  (struct sockaddr*)&server_addr, &sockAddrSize)));
	    printf("recv [%d] bytes: %s.\r\n", rbytes , recv_buf);
   	}
    close(client_fd);
}

void ipv4_udp_server_test(void)
{
    int recvLen;
    int serverFd;
    char msg[128]={0};
    socklen_t addrLen;
    struct sockaddr_in serverAddr;
	struct sockaddr_in  clientAddr;
    int rbytes = -1;

    printf("ipv4_udp_server_test !!\r\n");    

    serverFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (serverFd < 0) {
        printf("ipv4_udp_server_test socket failed\n");
        return;
    }
    else
    {
        printf("ipv4_udp_server_test socket success\n");
    }
    addrLen = sizeof(struct sockaddr_in);
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
	serverAddr.sin_len = addrLen;
    serverAddr.sin_addr.s_addr = htonl (INADDR_ANY); 

	clientAddr.sin_len = (u_char) addrLen; 
    clientAddr.sin_family = AF_INET; 
    clientAddr.sin_port = htons(SERVER_PORT);
    if (inet_pton(AF_INET, SERVER_ADDR, &clientAddr.sin_addr) <= 0) {
        printf("inet_pton error!!!\n");
        return;
    }
    
    if (bind(serverFd, (struct sockaddr*)&serverAddr, addrLen) < 0) {
        printf("ipv4_udp_server_test bind failed\n");
        return;
    }
    else
    {
        printf("ipv4_udp_server_test bind success\n");
    }
	
    printf("ipv4_udp_server_test recv form\n");
    //fflush(stdout);
    while (1) {
		sprintf (msg, "%s", " udp server example\n");
		rbytes = sendto(serverFd, msg, strlen(msg), 0,(struct sockaddr *)&clientAddr, sizeof(struct sockaddr_in));
	    if (rbytes > 0) {
	        printf("send to server: %d bytes written\n> %s\n", rbytes, msg);
	    } else {
	        printf("send failed\n");
	    }
        memset(msg, 0, sizeof(msg));
        recvLen = recvfrom(serverFd, msg, sizeof(msg), 0, (struct sockaddr*)&clientAddr, &addrLen);
        if (recvLen > 0) {
            printf("%d bytes \n>%s\n", recvLen, msg);
            continue;
        }
    }

    close(serverFd);
}
void bsp_xec_iomux_cfg(void)
{
    iomux_ls_iof_oval_cfg(IOMUX_BASE, XEC_MDC_IOF_OVAL , 11, XEC_MDC_HS_SEL , 0 , 0);

    iomux_ls_iof_oval_cfg(IOMUX_BASE, XEC_MDIO_IOF_OVAL , 12, XEC_MDIO_HS_SEL , 0 , 0);
    iomux_ls_iof_ival_cfg(IOMUX_BASE, XEC_MDIO_IOF_IVAL , 12, XEC_MDIO_HS_SEL, 0, 0);
    //  iomux_ls_iof_inv_cfg(IOMUX_BASE, XEC_XMII_TXC_IOF_IVAL, XEC_XMII_TXC_HS_SEL ,0,0);
     
    iomux_iof_ival_cfg(IOMUX_BASE, XEC_XMII_TXC_IOF_IVAL , 0, XEC_XMII_TXC_HS_SEL ,0 , 0);
    // iomux_ls_iof_inv_cfg(IOMUX_BASE, XEC_GMII_RXC_IOF_OVAL, XEC_GMII_RXC_HS_SEL ,0,0);
    iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXC_IOF_OVAL , 0, XEC_GMII_RXC_HS_SEL, 0,  0);
    iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXEN_IOF_OVAL , 0, XEC_GMII_TXEN_HS_SEL , 0 , 0);
    iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXDV_IOF_OVAL , 0, XEC_GMII_RXDV_HS_SEL, 0, 0);

    // iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXER_IOF_OVAL , 0, XEC_GMII_TXER_HS_SEL, 0, 0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXER_IOF_IVAL , 0, XEC_GMII_RXER_HS_SEL, 0, 0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_CRS_IOF_IVAL , 0, XEC_GMII_CRS_HS_SEL, 0, 0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_COL_IOF_IVAL , 0, XEC_GMII_COL_HS_SEL, 0, 0);

     iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT0_IOF_OVAL ,0, XEC_GMII_TXD_BIT0_HS_SEL ,0 ,0);
    // iomux_ls_iof_slew_rate(IOMUX_BASE, XEC_GMII_TXD_BIT0_IOF_OVAL ,0, 0 ,0 ,0);
     iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT1_IOF_OVAL ,0, XEC_GMII_TXD_BIT1_HS_SEL ,0 ,0);
    // iomux_ls_iof_slew_rate(IOMUX_BASE, XEC_GMII_TXD_BIT1_IOF_OVAL ,0, 0 ,0 ,0);
     iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT2_IOF_OVAL ,0, XEC_GMII_TXD_BIT2_HS_SEL ,0 ,0);
    // iomux_ls_iof_slew_rate(IOMUX_BASE, XEC_GMII_TXD_BIT2_IOF_OVAL ,0, 0 ,0 ,0);
     iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT3_IOF_OVAL ,0, XEC_GMII_TXD_BIT3_HS_SEL ,0 ,0);
    // iomux_ls_iof_slew_rate(IOMUX_BASE, XEC_GMII_TXD_BIT3_IOF_OVAL ,0, 0 ,0 ,0);
    // iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT4_IOF_OVAL ,0, XEC_GMII_TXD_BIT4_HS_SEL ,0 ,0);
    // iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT5_IOF_OVAL ,0, XEC_GMII_TXD_BIT5_HS_SEL ,0 ,0);
    // iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT6_IOF_OVAL ,0, XEC_GMII_TXD_BIT6_HS_SEL ,0 ,0);
    // iomux_iof_oval_cfg(IOMUX_BASE, XEC_GMII_TXD_BIT7_IOF_OVAL ,0, XEC_GMII_TXD_BIT7_HS_SEL ,0 ,0);

    iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT0_IOF_OVAL ,0, XEC_GMII_RXD_BIT0_HS_SEL ,0 ,0);
    iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT1_IOF_OVAL ,0, XEC_GMII_RXD_BIT1_HS_SEL ,0 ,0);
    iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT2_IOF_OVAL ,0, XEC_GMII_RXD_BIT2_HS_SEL ,0 ,0);
    iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT3_IOF_OVAL ,0, XEC_GMII_RXD_BIT3_HS_SEL ,0 ,0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT4_IOF_OVAL ,0, XEC_GMII_RXD_BIT4_HS_SEL ,0 ,0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT5_IOF_OVAL ,0, XEC_GMII_RXD_BIT5_HS_SEL ,0 ,0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT6_IOF_OVAL ,0, XEC_GMII_RXD_BIT6_HS_SEL ,0 ,0);
    // iomux_iof_ival_cfg(IOMUX_BASE, XEC_GMII_RXD_BIT7_IOF_OVAL ,0, XEC_GMII_RXD_BIT7_HS_SEL ,0 ,0);
    ////////////////////yhf
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_0_IOF_IVAL,39,LGPIO_IO_PORT_PINS_0_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_1_IOF_IVAL,40,LGPIO_IO_PORT_PINS_1_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_2_IOF_IVAL,35,LGPIO_IO_PORT_PINS_2_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_3_IOF_IVAL,36,LGPIO_IO_PORT_PINS_3_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_4_IOF_IVAL,37,LGPIO_IO_PORT_PINS_4_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_5_IOF_IVAL,38,LGPIO_IO_PORT_PINS_5_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_4_IOF_IVAL,33,LGPIO_IO_PORT_PINS_6_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_5_IOF_IVAL,34,LGPIO_IO_PORT_PINS_7_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_3_IOF_IVAL,0,LGPIO_IO_PORT_PINS_3_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_4_IOF_IVAL,1,LGPIO_IO_PORT_PINS_4_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_5_IOF_IVAL,2,LGPIO_IO_PORT_PINS_5_HS_SEL,0,0);
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_24_IOF_IVAL,158,LGPIO_IO_PORT_PINS_24_HS_SEL,0,0);   
    // iomux_ls_iof_ival_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_25_IOF_IVAL,159,LGPIO_IO_PORT_PINS_25_HS_SEL,0,0);
    ////////////////////yhf
}
// extern struct     netif gnetif;
static UINT32 Example_TaskLo(VOID)
{
    // struct netif *Test_disnetif;
    // struct pbuf *Test_p;
    // struct eth_addr *Test_ethaddr;
    // gnetif.hwaddr[0] = 0x00;
    // gnetif.hwaddr[1] = 0x2B;
    // gnetif.hwaddr[2] = 0x20;
    // gnetif.hwaddr[3] = 0x21;
    // gnetif.hwaddr[4] = 0x03;
    // gnetif.hwaddr[5] = 0x23;

    // Test_disnetif->hwaddr[0] = 0x00;
    // Test_disnetif->hwaddr[1] = 0x0E;
    // Test_disnetif->hwaddr[2] = 0xC6;
    // Test_disnetif->hwaddr[3] = 0x2A;
    // Test_disnetif->hwaddr[4] = 0xA2;
    // Test_disnetif->hwaddr[5] = 0x17;

    // Test_p->next=NULL;
    // Test_p->len=8;
    // *((u8_t*)Test_p->payload)=0x01;
    // *((u8_t*)Test_p->payload+1)=0x02;
    // *((u8_t*)Test_p->payload+2)=0x03;
    // *((u8_t*)Test_p->payload+3)=0x04;
    // *((u8_t*)Test_p->payload+4)=0x05;
    // *((u8_t*)Test_p->payload+5)=0x06;
    // *((u8_t*)Test_p->payload+6)=0x07;
    // *((u8_t*)Test_p->payload+7)=0x08;


    UINT32 TempData = 0;
    printf("\r\n1. eth init success!\r\n");
    // LOS_TaskDelay(1000);
    // Example_Lwip_socket_success();
    // LOS_TaskDelay(1000);
    // Example_Lwip_socket_failed();
    // TempData=0;
    // printf("\r\n1. send 0xAA test Example_Lwip_TcpClient_Send_Success,port 1883!\r\n");
    // while(TempData!=0xAA)
    // {
    //     TempData=(USART0->RXDATA);
    //     if(TempData& USART_RXDATA_EMPTY)
    //     {
    //         TempData=0;
    //     }
    //     else
    //     {
    //         TempData=(TempData & 0xFF);
    //     }
    //     LOS_TaskDelay(1000);
    // }
    // Example_Lwip_TcpClient_Send_Success();

    // TempData=0; 
    // printf("\r\n1. send 0xAA test Example_Lwip_TcpServer_Send_Success,port 1884!\r\n");
    // while(TempData!=0xAA)
    // {
    //     TempData=(USART0->RXDATA);
    //     if(TempData& USART_RXDATA_EMPTY)
    //     {
    //         TempData=0;
    //     }
    //     else
    //     {
    //         TempData=(TempData & 0xFF);
    //     }
    //     LOS_TaskDelay(1000);
    // }
    // Example_Lwip_TcpServer_Send_Success();

    // TempData=0; 
    // printf("\r\n1. send 0xAA test Example_Lwip_TcpClient_Recv_Success,port 1885!\r\n");
    // while(TempData!=0xAA)
    // {
    //     TempData=(USART0->RXDATA);
    //     if(TempData& USART_RXDATA_EMPTY)
    //     {
    //         TempData=0;
    //     }
    //     else
    //     {
    //         TempData=(TempData & 0xFF);
    //     }
    //     LOS_TaskDelay(1000);
    // }
    // Example_Lwip_TcpClient_Recv_Success();

    // TempData=0; 
    // printf("\r\n1. send 0xAA test Example_Lwip_TcpServer_Recv_Success,port 1886!\r\n");
    // while(TempData!=0xAA)
    // {
    //     TempData=(USART0->RXDATA);
    //     if(TempData& USART_RXDATA_EMPTY)
    //     {
    //         TempData=0;
    //     }
    //     else
    //     {
    //         TempData=(TempData & 0xFF);
    //     }
    //     LOS_TaskDelay(1000);
    // }
    // Example_Lwip_TcpServer_Recv_Success();

    TempData=0; 
    printf("\r\n1. send 0xAA test Example_Lwip_UDP_sendto_Success,port 1887!\r\n");
    while(TempData!=0xAA)
    {
        TempData=(USART0->RXDATA);
        if(TempData& USART_RXDATA_EMPTY)
        {
            TempData=0;
        }
        else
        {
            TempData=(TempData & 0xFF);
        }
        LOS_TaskDelay(1000);
    }
    Example_Lwip_UDP_sendto_Success();

    TempData=0; 
    printf("\r\n1. send 0xAA test Example_Lwip_UDP_recvfrom_Success,port 1887!\r\n");
    while(TempData!=0xAA)
    {
        TempData=(USART0->RXDATA);
        if(TempData& USART_RXDATA_EMPTY)
        {
            TempData=0;
        }
        else
        {
            TempData=(TempData & 0xFF);
        }
        LOS_TaskDelay(1000);
    }
    Example_Lwip_UDP_recvfrom_Success();
    // printf("\r\n2. UdpRecvTest!\r\n");
    // UdpRecvTest();
    // printf("\r\n2. UdpSendTest!\r\n");
    // UdpSendTest();


    // while (g_XEC_flag==1)
    // {
    //     TempData=(USART0->RXDATA);
    //     if(TempData& USART_RXDATA_EMPTY)
    //     {
    //         TempData=0;
    //     }
    //     else
    //     {
    //         TempData=(TempData & 0xFF);
    //     }
    //     // TempData=((USART0->RXDATA)& 0xFF);
    //     if((TempData==0xA9)||(TempData==0x59))
    //     {
    //         g_XEC_flag=0;
    //     }
    //     else if(TempData==0x77)
    //     {
    //         ipv4_udp_test();        
    //     }
    //     else if(TempData==0x88)
    //     {
    //         ipv4_udp_server_test();
    //     }
    //     else if(TempData==0x99)
    //     {
    //         ipv4_tcp_test();
    //     }
    //     else if(TempData==0x11)
    //     {
    //         ipv4_tcp_server_test();
    //     }
    //     TempData=0;
    //     printf("\r\n2. send '0xA9' or '0x59' to finish the test!\r\n");
    //     LOS_TaskDelay(3000);
    // }
    printf("\r\n3. BSP XEC test success!\r\n");
    g_XEC_flag=1;
    LOS_InspectStatusSetByID(LOS_INSPECT_BSP_XEC,LOS_INSPECT_STU_SUCCESS);
    LOS_TaskDelete(g_uwTskHiID);
    LOS_TaskDelete(g_uwTskLoID);

    return LOS_OK;
}
extern int link_main(void *args);
UINT32 Example_BSP_XEC(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam;
    
    printf("\r\n--------4.27.9 BSP XEC test start!--------\r\n");
    /* lock task shcedue */
    LOS_TaskLock();

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)link_main;
    stInitParam.usTaskPrio = TSK_PRIOR_HI;
    stInitParam.pcName = "link_test";
    stInitParam.uwStackSize = 0x1000;
    /* create high prio task */
    uwRet = LOS_TaskCreate(&g_uwTskHiID, &stInitParam);
    if (uwRet != LOS_OK)
    {
        LOS_TaskUnlock();

        printf("\r\nlink_test create Failed!\r\n");
        return LOS_NOK;
    }

    printf("\r\n1. link_test create Success!\r\n");

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_TaskLo;
    stInitParam.usTaskPrio = TSK_PRIOR_LO;
    stInitParam.pcName = "link_fpga";
    stInitParam.uwStackSize = 0x1000;
    /* create low prio task */
    uwRet = LOS_TaskCreate(&g_uwTskLoID, &stInitParam);
    if (uwRet != LOS_OK)
    {
        LOS_TaskUnlock();

        printf("\r\nxec create Failed!\r\n");
        return LOS_NOK;
    }
    printf("\r\n2. xec create Success!\r\n");
    bsp_xec_iomux_cfg();
    ETH_Init();
    /* unlock task schedue */
    LOS_TaskUnlock();

    return uwRet;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
